home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Calling a function by reference?!
- Date: 11 Mar 1996 11:33:42 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4i1v6mINN5fk@keats.ugrad.cs.ubc.ca>
- References: <ga16wMlyZAQF088yn@ime.usp.br>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <ga16wMlyZAQF088yn@ime.usp.br>,
- Rogerio Brito <rbrito@ime.usp.br> wrote:
- > Hello, All.
- >
- > I don't know if this question is so dumb, but this is a
- > thing that is interesting from my point of view (that is,
- > from the point of view of a person that is not very much
- > experienced):
- >
- > How do I declare a function (say, foo) that receives two
- > strings and does modifications to both?
- >
- > Is there a better way than:
- >
- > void foo(char **argument1, char **argument2);
- >
- > ?? (It seems to be a dirty way... :( )
-
- No. You may need to do this if the length of these strings can be changed by
- the function. If the function is length-preserving, you can just pass a pointer
- to the first character.
-
- If you allow length changes, the function can only work with dynamic strings.
- You can't change the location of a character array, whether it is static or
- automatic. The routine will probably use malloc() and possibly realloc() to
- increase or decrease the size of the arguments.
-
- Another way is to explicitly give the user a function which computes the size
- of the destination operand. For example, if you are concatenating the strings
- (which is already done by the standard library), you know that the size of the
- result is one less than the sum of the sizes of the two original strings (due
- to the deletion of one zero character). Since the user knows this, you can set
- up the function in such a way that the user provides enough space to store a
- result, and the two arguments are not modified. This gives the user freedom to
- use space obtained by calling malloc, or use static or automatic storage.
- --
-
-